/***************************************************************************
** 版权所有:   Copyright (c) 2020-2030
** 文件名称:  showmutex.c
** 当前版本:  v1.0
** 作    者:
** 完成日期: 2025-02-13
** 修改记录:
** 修改日期:
** 版本号  :
** 修改人  :
** 修改内容:
***************************************************************************/

/*****************************头文件****************************************/
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/sched/prio.h>
#include <linux/pid.h>
#include <linux/mutex.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/pid_namespace.h>
/*****************************宏定义****************************************/

/*****************************结构体或类型定义*******************************/

/*****************************全局变量****************************************/

/*****************************本地变量****************************************/

/*****************************函数或类声明****************************************/

/*****************************函数或类实现****************************************/
// 显示所有进程和线程持有的互斥信号量
int showmutex(const char *param)
{
    struct task_struct *task;
    struct task_struct *owner_task;
    struct mutex *mutex;
    struct list_head *entry;

    printk(KERN_INFO "All mutexes held by processes and threads:\n");
    printk(KERN_INFO "PID\tThread Name\tMutex Address\tOwner PID\tOwner Thread Name\n");

    // 遍历所有进程
    for_each_process(task)
    {
        if (list_empty(&task->pi_state_list)) {
            printk(KERN_INFO "No mutexes held by process %d\n", task_pid_nr(task));
            continue;
        }

        // 遍历进程持有的互斥信号量
        list_for_each(entry, &task->pi_state_list)
        {
            mutex = list_entry(entry, struct mutex, wait_list);
            pid_t owner_pid = atomic_long_read(&mutex->owner);

            printk(KERN_INFO "Mutex owner PID: %ld\n", owner_pid);

            if (owner_pid > 0)
            {
                owner_task = pid_task(find_get_pid(owner_pid), PIDTYPE_PID);
                if (owner_task)
                {
                    printk(KERN_INFO "%d\t%s\t%p\t%d\t%s\n",
                           task_pid_nr(task), task->comm, mutex,
                           task_pid_nr(owner_task), owner_task->comm);
                }
                else
                {
                    printk(KERN_INFO "Owner task not found for PID: %d\n", owner_pid);
                }
            }
        }
    }

    return 0;
}

// 显示所有进程和线程持有的信号量
int showsem(const char *param)
{
    struct task_struct *task;
    struct semaphore *sem;
    struct list_head *entry;

    printk(KERN_INFO "All semaphores held by processes and threads:\n");
    printk(KERN_INFO "PID\tThread Name\tSemaphore Address\tCount\n");

    // 遍历所有进程
    for_each_process(task)
    {
        // 遍历进程持有的信号量
        list_for_each(entry, &task->pi_state_list)
        {
            sem = list_entry(entry, struct semaphore, wait_list);
            printk(KERN_INFO "%d\t%s\t%p\t%d\n",
                   task_pid_nr(task), task->comm, sem, sem->count);
        }
    }

    return 0;
}

